package info.sgu.ttdt;

import java.util.ArrayList;
import java.util.List;

import info.androidhive.slidingmenu.R;
import info.sgu.ttdt.adapter.ArrayAdapterItem;
import info.sgu.ttdt.adapter.DatabaseHelper;
import info.sgu.ttdt.model.BookMark;
import android.app.ListFragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class BookMarkFragment extends ListFragment {

	DatabaseHelper db;
	List<BookMark> datalist;
	//ArrayList<HashMap<String, String>> dataList;
	Button btn_Delete;
	//private static final String tag_masinhvien = "masinhvien";
	//private static final String tag_tensinhvien = "tensinhvien";
	//private static final String tag_loai = "loai";
	
	public BookMarkFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_bookmark,
				container, false);
		SharedPreferences settings = getActivity()
				.getSharedPreferences("UserInfo", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("type", 5);
		editor.commit();
		db = new DatabaseHelper(getActivity());
		datalist = new ArrayList<BookMark>();
		//dataList = new ArrayList<HashMap<String, String>>();
		ReLoad();
		
		return rootView;
	}
	
	/*
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		BookMark bm = datalist.get(position);
		String masv = bm.getMasinhvien();
		String tensv = bm.getTensinhvien();
		String loai = bm.getLoai();
		int pos = 0;
		if(loai.equals("Thời khóa biểu"))
			pos = 2;
		else if(loai.equals("Điểm thi"))
			pos = 3;
		else if(loai.equals("Lịch thi"))
			pos = 4;
		SharedPreferences settings = getActivity()
				.getSharedPreferences("UserInfo", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("masinhvien", masv);
		editor.putString("tensinhvien", tensv);
		editor.putInt("type", pos);
		editor.commit();
		((MainActivity)getActivity()).boomarkView(pos);
	}
	*/
	
	@Override
	public void onListItemClick(ListView l, View v, int pos, long id) {
	  super.onListItemClick(l, v, pos, id);
          Toast.makeText(getActivity(),v.getId()+"", Toast.LENGTH_SHORT).show();
	}
	
	
	public void ReLoad()
	{
		//dataList.clear();
		datalist = db.getBookmarks();
		/*
		for (BookMark bm : datalist) {
			HashMap<String, String> data = new HashMap<String, String>();
			data.put(tag_masinhvien,
					"Mã sinh viên: " + bm.getMasinhvien());
			data.put(tag_tensinhvien,
					"Tên sinh viên: " + bm.getTensinhvien());
			data.put(tag_loai,
					"Loại: " + bm.getLoai());
			// adding contact to contact list
			dataList.add(data);
		}
		
		
		ListAdapter adapter = new SimpleAdapter(getActivity(),
				dataList, R.layout.list_item6, new String[] {
						tag_masinhvien, tag_tensinhvien, tag_loai}, new int[] {
						R.id.text1, R.id.text2, R.id.text3  });

		setListAdapter(adapter);
		*/
		ArrayAdapterItem adapter = new ArrayAdapterItem(getActivity(), datalist, BookMarkFragment.this);
		setListAdapter(adapter);
	}
	
	public void View(BookMark bm)
	{
		String masv = bm.getMasinhvien();
		String tensv = bm.getTensinhvien();
		String loai = bm.getLoai();
		int pos = 0;
		if(loai.equals("Thời khóa biểu"))
			pos = 2;
		else if(loai.equals("Điểm thi"))
			pos = 3;
		else if(loai.equals("Lịch thi"))
			pos = 4;
		SharedPreferences settings = getActivity()
				.getSharedPreferences("UserInfo", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("masinhvien", masv);
		editor.putString("tensinhvien", tensv);
		editor.commit();
		((MainActivity)getActivity()).boomarkView(pos);
	}
	
}
